# 使用官方 Node.js 运行时
FROM registry.cn-shenzhen.aliyuncs.com/devops_irlab/node:20-alpine3.20 AS builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 从docker compose传递过来的参数传递到web中
ARG NEXT_PUBLIC_API_URL  
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL  
# 安装 pnpm
# RUN corepack enable && corepack prepare pnpm@latest --activate
RUN npm install -g pnpm

# p配置yarn淘宝镜像
RUN pnpm config set registry https://registry.npmmirror.com/
RUN npm config set registry https://registry.npmmirror.com/
RUN yarn config set registry https://registry.npmmirror.com/

WORKDIR /app

# 复制 package.json 和 pnpm-lock.yaml
COPY package.json pnpm-lock.yaml ./

# 安装依赖
RUN pnpm install --frozen-lockfile

# 复制所有文件
COPY . .

# 构建 Next.js 项目
RUN pnpm build

# 生产环境运行容器
FROM registry.cn-shenzhen.aliyuncs.com/devops_irlab/node:20-alpine3.20 AS runner
WORKDIR /app

# RUN corepack enable && corepack prepare pnpm@latest --activate
RUN npm install -g pnpm

# 复制构建结果
COPY --from=builder /app/.next .next
COPY --from=builder /app/node_modules node_modules
COPY --from=builder /app/public public
COPY --from=builder /app/package.json .

EXPOSE 3000

# CMD ["sh", "-c", "pnpm start -- -p 3000"]
CMD ["sh", "-c", "pnpm start -p 3000"]

